/*******************************************************************************
 * Copyright (c) 2007 University of Oslo.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     University of Oslo - initial API and implementation
 *******************************************************************************/

package com.cea.papyrus.diagram.sequence.sedi.edit;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import org.eclipse.draw2d.Figure;
import org.eclipse.draw2d.FreeformLayer;
import org.eclipse.draw2d.FreeformLayout;
import org.eclipse.draw2d.IFigure;
import org.eclipse.emf.ecore.EObject;
import org.eclipse.gef.EditPolicy;
import org.eclipse.gef.GraphicalEditPart;
import org.eclipse.uml2.uml.Element;
import org.eclipse.uml2.uml.Interaction;

import com.cea.papyrus.core.editor.ModelManager;
import com.cea.papyrus.core.editpart.IUMLDiagramElementEditPart;
import com.cea.papyrus.diagram.sequence.sedi.edit.policy.DiagramLayoutPolicy;
import com.cea.papyrus.diagraminterchange2.di2.Diagram;
import com.cea.papyrus.diagraminterchange2.di2.DiagramElement;
import com.cea.papyrus.diagraminterchange2.di2.GraphElement;
import com.cea.papyrus.diagraminterchange2.di2.GraphNode;

/**
 * Edit part for Diagram.
 * 
 * @author Andreas Limyr
 * @author frankd@ifi.uio.no
 */
public class DiagramEditPart extends com.cea.papyrus.core.editpart.DiagramEditPart 
implements GraphicalEditPart, IUMLDiagramElementEditPart {

    public DiagramEditPart(Diagram model, ModelManager modelManager) {
        super(model, modelManager);
    }

    @Override
    protected IFigure createFigure() {
        Figure f = new FreeformLayer();
        f.setLayoutManager(new FreeformLayout());
        f.setOpaque(true);
        return f;
    }

    @Override
    protected void createEditPolicies() {
        installEditPolicy(EditPolicy.LAYOUT_ROLE, new DiagramLayoutPolicy());
    }

	@Override
	protected List<GraphElement> getModelChildren() {
		ArrayList<GraphElement> contained= new ArrayList<GraphElement>();
			
		if (getGraphNode() != null) {
			Iterator<DiagramElement> it=getGraphNode().getContained().iterator();
			while (it.hasNext()){
				GraphElement ge=(GraphElement) it.next();
				// The only child should be the interaction in most of the case
				if (ge instanceof GraphNode) {
					contained.add(ge);	
				}
			}
		}
		
		return contained;
	}
	
    public Element getUMLModel() {
        // There is no direct link to the associated Interaction.
        // The diagram is only added as an attachment to Interaction
        // so we can search outwards.
        Diagram diagram = (Diagram) getModel();
        EObject parent = diagram.eContainer();
        while (parent != null && !(parent instanceof Interaction))
            parent = parent.eContainer();

        return (parent instanceof Interaction) ? (Interaction) parent : null;
    }

	public Diagram getDiagram() {
		return (Diagram) getModel();
	}
}
